package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"seat-service/initialization"
	"seat-service/model"
	"seat-service/model/dto"
	"seat-service/response"
	service "seat-service/service/impl"
	"strconv"
)

type AuthApi struct{}

var casbinService service.CasbinService

// UpdateUserRole 添加用户组策略,
func (a *AuthApi) UpdateUserRole(context *gin.Context) {
	var addRole dto.AddRoleParams
	//接收请求参数
	if err := context.ShouldBindJSON(&addRole); err != nil {
		resp.Fail(context, response.CodeParamError, nil)
		return
	}
	err := casbinService.CreateUser(addRole.RoleCode, addRole.Role, addRole.Ordered, addRole.Status)
	if err != nil {
		if err.Error() == "RoleISExist" {
			resp.Fail(context, response.CodeUserExist, nil)
			return
		}
		initialization.SeatLogger.Error(fmt.Sprintf("utils.Casbin.UpdateUserRole(username, rolename) is failed : %v\n", err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	resp.Success(context, response.CodeSuccess, nil)
}

// GetAllRoles 得到所有角色
func (a *AuthApi) GetAllRoles(context *gin.Context) {
	var pageParams dto.PageParam
	if err := context.ShouldBindJSON(&pageParams); err != nil {
		resp.Fail(context, response.CodeParamError, nil)
		return
	}
	err, allroles, total := casbinService.SelectRoles(pageParams)
	if err != nil {
		initialization.SeatLogger.Error("casbinService.SelectRoles(pageParams) is failed", zap.Error(err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	respData := struct {
		AllRoles []model.CasbinRuleExtended `json:"all_roles"`
		Total    int64                      `json:"total"`
	}{
		AllRoles: allroles,
		Total:    total,
	}
	resp.Success(context, response.CodeSuccess, respData)
}

// GetRoleById 根据id 查询角色信息
func (a *AuthApi) GetRoleById(context *gin.Context) {
	params := context.Query("id") //角色id
	roleId, err := strconv.Atoi(params)
	if err != nil {
		resp.Fail(context, response.CodeParamError, nil)
		return
	}
	role, err := casbinService.SelectRoleById(roleId)
	if err != nil {
		initialization.SeatLogger.Error("casbinService.SelectRoleById(roleId) is error", zap.Error(err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	resp.Success(context, response.CodeSuccess, role)
}

// ChangeRoleById 根据id 修改角色信息
func (a *AuthApi) ChangeRoleById(context *gin.Context) {
	var newRoleInformation dto.ChangeRole
	if err := context.ShouldBindJSON(&newRoleInformation); err != nil {
		resp.Fail(context, response.CodeParamError, nil)
		return
	}
	role, err := casbinService.ChangeRoleById(newRoleInformation)
	if err != nil {
		initialization.SeatLogger.Error("casbinService.ChangeRoleById(newRoleInformation) is error", zap.Error(err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	resp.Success(context, response.CodeSuccess, role)
}

// DeleteRoleByIds 删除角色
func (a *AuthApi) DeleteRoleByIds(context *gin.Context) {
	//接收请求参数
	var pararm dto.DeleteRole
	if err := context.ShouldBindJSON(&pararm); err != nil {
		resp.Fail(context, response.CodeParamError, nil)
		return
	}
	err := casbinService.DeleteRoleByIds(pararm.Ids)
	if err != nil {
		initialization.SeatLogger.Error("casbinService.DeleteRoleByIds(pararm.Ids) is failed", zap.Error(err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	resp.Success(context, response.CodeSuccess, nil)
}

// AllocateRule 分配权限
func (a *AuthApi) AllocateRule(context *gin.Context) {
	//接收请求参数
	var ruleInformation dto.RuleID
	if err := context.ShouldBindJSON(&ruleInformation); err != nil {
		resp.Fail(context, response.CodeParamError, nil)
		return
	}
	err := casbinService.Allocate(ruleInformation)       //更新casbin表中的数据
	err = menuService.ChangeControlRole(ruleInformation) //更新菜单表中的数据
	if err != nil {
		initialization.SeatLogger.Error("casbinService.Allocate(ruleInformation) is failed", zap.Error(err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	resp.Success(context, response.CodeSuccess, nil)
}
